package io.gitee.hchaojie.day05;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class TestList {
    @Test
    public void test2() {
        // 原始类型
        List list = new ArrayList();
        
        String s = "abc";
        
        list.add(s);
        list.add(s);

        System.out.println(list);
    }

    @Test
    public void test4() {
        List<String> list = new ArrayList<>();

        list.add("abc");
        list.add("def");
        list.add("abc");

        System.out.println(list);

        list.remove("abc");

        System.out.println(list);
    }

    // 老师没有实现eauals方法，remove（）删除不了
    @Test
    public void test5() {
        List<Teacher> teachers = new ArrayList<>();

        teachers.add(new Teacher("黄老师"));
        teachers.add(new Teacher("付老师"));

        System.out.println(teachers);

        teachers.remove(new Teacher("黄老师"));
        System.out.println(teachers);
    }
    
    @Test
    public void test6() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 18, "10001"));
        students.add(new Student("李四", 18, "10001"));
        
        System.out.println(students);
        
        students.remove(new Student("张三", 18, "10001"));
        System.out.println(students);
        
        if (students.contains(new Student("李四", 18, "10001"))) {
            System.out.println("有。。。");
        }
    }

    @Test
    public void testIterator() {
        List<String> list = new ArrayList<>();

        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhanliu");

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        // 增强的for循环, foreach
        for (String s : list) {
            System.out.println(s);
        }

        // 迭代器模式
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();  // 指针指向下一位
            System.out.println(next);
        }
    }
    
    // 遍历时删除某个元素
    @Test
    public void testRemove() {
        List<String> list = new ArrayList<>();

        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhanliu"); 

        // 删除第一个、最后一个会报错
        for (String s : list) {
            if ("zhangsan".equals(s)) {
                list.remove(s);
            }
        }
        
        System.out.println(list);
    }

    // 遍历时删除某个元素
    @Test
    public void testRemoveSafely() {
        List<String> list = new ArrayList<>();

        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhanliu");

        Iterator<String> iter = list.iterator();        // 获取list的迭代器
        while (iter.hasNext()) {
            String next = iter.next();

            if ("wangwu".equals(next)) {
                iter.remove();      // 删除当前位置处的元素
            }
        }

        System.out.println(list);
    }

    // 泛型，指定容器里面元素的类型
    @Test
    public void test3() {
        List<String> list = new ArrayList<>();

        list.add("abc");
        list.add("def");

        List<Integer> list2 = new ArrayList<>();
    }
    
    @Test
    public void test() {
        Assert.assertArrayEquals(new int[] {2}, findPrimeIn(2));
        Assert.assertArrayEquals(new int[] {2, 3}, findPrimeIn(3));
        Assert.assertArrayEquals(new int[] {2, 3, 5, 7}, findPrimeIn(10));

        findPrimeIn(100);
    }

    @Test
    public void testList() {
        List list = findPrimeInResultList(100);

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public List findPrimeInResultList(int max) {
        if (max < 2) {
            return null;
        }

        List list = new ArrayList();
        for (int i = 2; i < max; i++) {
            if (isPrime(i)) {
                list.add(i);
            }
        }

        return list;
    }

    // 写一个方法，找出100以内的所有质数并返回。
    public int[] findPrimeIn(int max) {
        if (max < 2) {
            return new int[] {};
        }

        int[] result = new int[max];

        int index = 0;

        for (int i = 2; i <= max; i++) {
          if (isPrime(i)) {
            result[index] = i;
            index++;
          }
        }

        return Arrays.copyOf(result, index);
    }

    /**
     * 判断参数是否是质数
     * @param num
     * @return
     */
    public boolean isPrime(int num) {
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                return false;
            }
        }

        return true;
    }
}
